---
sidebar_position: 3
---

import FolderView from '@site/src/components/FolderView';

# i18n Guide

## Registering i18n for Plugins \{#registering-i18n-for-plugins}

Using the ExamplePlugin-Maven project as an example.

You need to register i18n in the main class of your plugin.

```java title="ExamplePlugin.java"
package cn.nukkitmot.exampleplugin;

import cn.nukkit.lang.PluginI18n;
import cn.nukkit.lang.PluginI18nManager;
import cn.nukkit.plugin.PluginBase;

public class ExamplePlugin extends PluginBase {
    public static ExamplePlugin INSTANCE;   
    public static PluginI18n I18N;
    public static LangCode serverLangCode;

    @Override
    public void onLoad() {
        //save Plugin Instance
        INSTANCE = this;
        //register the plugin i18n
        I18N = PluginI18nManager.register(this);
        initServerLangCode();
    }
    
    public void initServerLangCode() {
        switch (Server.getInstance().getLanguage().getLang()) {
            case "eng" -> {
                serverLangCode = LangCode.en_US;
            }
            case "chs" -> {
                serverLangCode = LangCode.zh_CN;
            }
            case "deu" -> {
                serverLangCode = LangCode.de_DE;
            }
            case "rus" -> {
                serverLangCode = LangCode.ru_RU;
            }
            default -> {
                try {
                    serverLangCode = LangCode.valueOf(Server.getInstance().getLanguage().getLang());
                } catch (IllegalArgumentException e) {
                    serverLangCode = LangCode.en_US;
                }
            }
        }
    }
}
```

Here, I will use `PlayerLocallyInitializedEvent` to demonstrate how to use:

```java title="EventListener.java"
package cn.nukkitmot.exampleplugin;

import cn.nukkit.Server;
import cn.nukkit.Player;
import cn.nukkit.lang.LangCode;
import cn.nukkit.event.player.PlayerLocallyInitializedEvent;

import static cn.nukkitmot.exampleplugin.ExamplePlugin.INSTANCE;
import static cn.nukkitmot.exampleplugin.ExamplePlugin.I18N;
import static cn.nukkitmot.exampleplugin.ExamplePlugin.serverLangCode;

public class EventListener implements Listener {
    @EventHandler
    public void joinEvent(PlayerLocallyInitializedEvent event) {
        Player player = event.getPlayer();
        LangCode playerLangCode = player.getLanguageCode();
        // This is how to get the player's LangCode
        player.sendMessage(I18N.tr(playerLangCode, "exampleplugin.helloworld", playerName));
        // We can also get the server's LangCode
        INSTANCE.getLogger().info(I18N.tr(serverLangCode, "exampleplugin.helloworld", playerName));
    }
}
```

## Adding Language Files to Resources \{#adding-language-files-to-resources}

Then we need to create a `language` folder under `src/main/resources` to store **\<[LangCode](#langcode)\>.json** language files. Your plugin development directory will look something like this:

<FolderView
    paths={[
    'ExamplePlugin-Maven/lib',
    'ExamplePlugin-Maven/src/main/java',
    'ExamplePlugin-Maven/src/main/resources/language/en_US.lang',
    'ExamplePlugin-Maven/target',
]}
>
</FolderView>

### language files format \{#language-files-format}

Support for multilingual text of parameters,such as `exampleplugin.helloworld=Hello {%0}!`

\{%n\} will be populated with arguments with index n

```ini
exampleplugin.helloworld=Hello {%0}!
exampleplugin.examplecommand.description=this is a test command.
```

## LangCode \{#langcode}

Refer to [cn.nukkit.lang.LangCode](https://github.com/MemoriesOfTime/Nukkit-MOT/blob/master/src/main/java/cn/nukkit/lang/LangCode.java). Here's a table listing the codes for easy reference:

:::note
Here, 'Region' does not represent a country.
:::

| Language Code | Language | Region |
|---------------|----------|--------|
| en_US   | English  | United States |
| en_GB   | English | United Kingdom |
| de_DE   | Deutsch | Deutschland |
| es_ES   | Español | España |
| es_MX   | Español | México |
| fr_FR   | Français | France |
| fr_CA   | Français | Canada |
| it_IT   | Italiano | Italia |
| ja_JP   | 日本語 | 日本 |
| ko_KR   | 한국어 | 대한민국 |
| pt_BR   | Português | Brasil |
| pt_PT   | Português | Portugal |
| ru_RU   | Русский | Россия |
| zh_CN   | 中文(简体) | 中国 |
| zh_TW   | 中文(繁體) | 中国台湾 |
| nl_NL   | Nederlands | Nederland |
| bg_BG   | Български | България |
| cs_CZ   | Čeština | Česko |
| da_DK   | Dansk | Danmark |
| el_GR   | Ελληνικά | Ελλάδα |
| fi_FI   | Suomi | Suomi |
| hu_HU   | Magyar | Magyarország |
| id_ID   | Indonesia | Indonesia |
| nb_NO   | Norsk bokmål | Norge |
| pl_PL   | Polski | Polska |
| sk_SK   | Slovenčina | Slovensko |
| sv_SE   | Svenska | Sverige |
| tr_TR   | Türkçe | Türkiye |
| uk_UA   | Українська | Україна |